package com.bipa.controller;

import com.bipa.bean.ShopCar;
import com.bipa.bean.User;
import com.bipa.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class ShopCarController {

    @Autowired
    ShopCarService shopCarService;

    @RequestMapping("/showShopCar")
    public void showShopCar(@SessionAttribute(value = "session_user",required = false) User user, HttpServletRequest request, HttpServletResponse response) throws Exception{
        //User user = (User) request.getSession().getAttribute("session_user");
        if (user == null){
            request.setAttribute("msg", "您尚未登录，请登录后在进行该操作！");
            request.getRequestDispatcher("/WEB-INF/view/front/login.jsp").forward(request,response);
        }else {
            List<ShopCar> shopcars = shopCarService.getShopCarByUserId(user.getId());
            request.setAttribute("shopcars",shopcars);
            request.setAttribute("highLight","carby");
            request.getRequestDispatcher("/WEB-INF/view/front/shopcar.jsp").forward(request,response);
        }
    }

    @RequestMapping("/addToCar")
    public void addToCar(Integer id,Integer number,HttpServletRequest request,HttpServletResponse response) throws Exception{
        User user = (User) request.getSession().getAttribute("session_user");
        if (user == null){
            request.setAttribute("msg", "您尚未登录，请登录后在进行该操作！");
            request.getRequestDispatcher("/WEB-INF/view/front/login.jsp").forward(request,response);
        }else {
            if (shopCarService.getShopCarByUserIdAndArticleId(user.getId(),id)){
                shopCarService.updateShopcar(user.getId(),id,number);
            }else {
                shopCarService.addShopcar(user.getId(),id,number);
            }
            response.sendRedirect("showShopCar");
        }
    }

    @RequestMapping("/updateToCar")
    public void updateToCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") Integer articleId,@RequestParam("buyNum") Integer buyNum) throws ServletException, IOException {

        User user = (User)request.getSession().getAttribute("session_user");
        if (user == null){
            request.setAttribute("message", "您尚未登录，请登录后在进行该操作！");
            request.getRequestDispatcher("/WEB-INF/view/front/login.jsp").forward(request, response);
        }else {

            shopCarService.updateShopcarTow(user.getId(),articleId,buyNum);
            response.sendRedirect("showShopCar");
        }
    }

    @RequestMapping("/deleteShopCarById")
    public void deleteShopCarById(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") Integer articleId) throws IOException, ServletException {
        User user = (User)request.getSession().getAttribute("session_user");
        if (user == null){
            request.setAttribute("message", "您尚未登录，请登录后在进行该操作！");
            request.getRequestDispatcher("/WEB-INF/view/front/login.jsp").forward(request, response);
        }else {

            shopCarService.deleteShopCarById(user.getId(),articleId);
            response.sendRedirect("showShopCar");
        }
    }
}
